package com.xiaohai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohai.projo.Class;
import com.xiaohai.mapper.ClassMapper;
import com.xiaohai.projo.dto.ClassDto;
import com.xiaohai.projo.Teacher;
import com.xiaohai.service.ClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohai.service.StudentService;
import com.xiaohai.service.TeacherService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaohai
 * @since 2024-02-24
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public Long getByTeacherId(Integer id) {
        LambdaQueryWrapper<Class> classLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classLambdaQueryWrapper.eq(id != null, Class::getTeaId, id);
        return super.count(classLambdaQueryWrapper);
    }

    @Override
    public IPage<ClassDto> myList(Integer currentPage, Integer pageSize, String name, String teacherName, Integer identity,String account) {
        Page<ClassDto> page = new Page<>(currentPage, pageSize);
        IPage<ClassDto> page1 = switch (identity) {
            case 1 -> classMapper.list(page, name, teacherName);
            case 2 -> classMapper.listByStu(page, name, teacherName, account);
            case 3 -> classMapper.listByTea(page, name, account);
            default -> null;
        };

        return page1;
    }

    @Override
    public ClassDto detail(String id) {
        ClassDto aClass = classMapper.getById(id);

        return aClass;
    }

    @Override
    public boolean add(Class aclass, Integer identity,String account) {
        if (identity.equals(3)) {// 教师
            Teacher teacher = teacherService.getInfoByAccount(account);
            aclass.setTeaId(teacher.getId());
        }
        return super.save(aclass);
    }

    @Override
    public Boolean myUpdate(Class aclass) {

        return super.updateById(aclass);
    }

    @Override
    public boolean delete(Integer id) {
        // 查看班级是否存在学生
        Boolean emptyByClassId = studentService.isEmptyByClassId(id);
        if (emptyByClassId) {
            return super.removeById(id);
        }
        return false;
    }

    @Override
    public Boolean batchAdd(List<Class> aclass, Integer identity) {
        if (identity.equals(3)) {// 教师
            String account = (String) request.getSession().getAttribute("account");
            Teacher teacher = teacherService.getInfoByAccount(account);
            aclass.forEach(a -> a.setTeaId(teacher.getId()));
        }
        boolean b = super.saveBatch(aclass);
        return b;
    }
}
